package cn.harmonyoslearn.navrouter.actions;

import cn.harmonyoslearn.navrouter.datas.NavrouterInfo;
import cn.harmonyoslearn.navrouter.datas.enums.InitNavrouterModelEnum;
import cn.harmonyoslearn.navrouter.dialog.NewNavigationPageDialog;
import cn.harmonyoslearn.navrouter.datas.PageOrDialogInfo;
import cn.harmonyoslearn.navrouter.models.NavrouterInfoModel;
import cn.harmonyoslearn.navrouter.models.ServiceModel;
import cn.harmonyoslearn.navrouter.utils.FileUtil;
import cn.harmonyoslearn.navrouter.utils.LogUtil;
import cn.harmonyoslearn.navrouter.utils.NotificationUtil;
import cn.harmonyoslearn.navrouter.utils.UpdateUtil;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public class NewNavPageAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        NewNavigationPageDialog dialog = new NewNavigationPageDialog(e);
        dialog.show();


        if (dialog.isOK()) {
            addFile(dialog.getInputData(), e);
        }
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        UpdateUtil.projectViewPopupMenuUpdate(e);
    }

    private void addFile(PageOrDialogInfo info, @NotNull AnActionEvent e) {
        try {
            // 校验文件名称是否为空，如果为空则提示
            if (Objects.equals(info.pageName, "")) {
                Messages.showMessageDialog("PageName is Null", "Error", Messages.getInformationIcon());
                return;
            }

            // 获取到需要创建的文件夹
            VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
            String filePath;
            if (file != null) {
                filePath = FileUtil.getNotNameFilePath(file.getPath());
            } else {
                LogUtil.i("NewNavPageAction.addFile","文件不存在");
                return;
            }



            // 获取内容并写入到项目中
            String projectContent = getPageContent(info);
            NavrouterInfo navrouterInfo = NavrouterInfoModel.getNavrouterInfoMap(Objects.requireNonNull(e.getProject()).getBasePath());
            // 自定义的直接引用 @tangs/navrouter
            if (navrouterInfo.model == InitNavrouterModelEnum.CUSTOM) {
                projectContent = projectContent.replace("\"navrouter\"", "\"@tangs/navrouter\"");
            }
            FileUtil.write(filePath, info.pageName + ".ets", projectContent, false);



            try {
                ServiceModel.getInstance().addPage(info.pageName, info.note, filePath + "/" + info.pageName + ".ets");
            } catch (Exception exception) {
                LogUtil.e("NewNavPageAction.addFile", "error：" + exception.getMessage());
            }

            // 打开编辑窗口
            FileUtil.openFile(e.getProject(), filePath + "/" + info.pageName + ".ets");
        } catch (Exception exception) {
            LogUtil.e("NewNavPageAction.addFile", "error：" + exception.getMessage());
        }
    }

    private String getPageContent(PageOrDialogInfo info) {
        String fileName = "AcquiescePage.ets";
        if (info.pageType == NewNavigationPageDialog.PageType.DIALOG) {
            fileName = "AcquiesceDialog.ets";
        }

        // 获取到对应模板的文本内容
        String resourcePath = "navrouterPageOrDialogFile/" + fileName;
        String content = FileUtil.readResourceFileContent(resourcePath);


        // 将内容替换一下  $$PageName$$   $$PageTittle$$
        content = content.replace("$$PageName$$", info.pageName);
        if (info.pageType == NewNavigationPageDialog.PageType.PAGE && info.isTittle) {
            content = content.replace("$$PageTittle$$", "\n      tittle: this.pageName,");
        } else {
            content = content.replace("$$PageTittle$$", "");
        }


        // 如果有填注释则替换 $$note$$
        if (!Objects.equals(info.note, "")) {
            content = content.replace("$$note$$", "/**\n * " + info.note + "\n */");
        } else {
            content = content.replace("$$note$$", "");
        }

        return content;
    }
}
























